//
//  ShenSuoTableCell.m
//  OCDemo
//
//  Created by BH on 15/10/20.
//  Copyright © 2015年 baohan. All rights reserved.
//

#import "ShenSuoTableCell.h"
#import <Masonry/Masonry.h>

@interface ShenSuoTableCell ()
{
    CGFloat height;
    CGFloat width;
}

@end

@implementation ShenSuoTableCell

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier text:(NSString *)text frame:(CGRect)frame
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
//        [self mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.top.equalTo(@0);
//            make.height.equalTo(@60);
//            make.width.equalTo(@(frame.size.width-45));
//        }];
        
        _label = [[UILabel alloc] init];
        _label.text = text;
        _label.font = [UIFont systemFontOfSize:14.f];
        [self.contentView addSubview:_label];
        
        [_label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(5);
            make.top.equalTo(self).offset(2);
            make.width.equalTo(@(frame.size.width-45));
            make.height.equalTo(@50);
        }];
        
        CGSize titleSize = [text boundingRectWithSize:CGSizeMake(frame.size.width-45, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.f]} context:nil].size;
        
        height = titleSize.height;
        width = titleSize.width;
        
        if (titleSize.height > 50) {
            
            _label.numberOfLines = 2;
            [_label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(@(titleSize.width));
            }];
            
            _btn = [[ShenSuoButton alloc] init];
            [_btn setImage:[UIImage imageNamed:@"double-down@2x"] forState:UIControlStateNormal];
            [_btn addTarget:self action:@selector(cellBtnClick) forControlEvents:UIControlEventTouchUpInside];
            [self.contentView addSubview:_btn];
            
            [_btn setIsEXP:YES];
            
            [_btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self).offset(frame.size.width-45);
                make.top.equalTo(@30);
                make.width.equalTo(@50);
                make.height.equalTo(@20);
            }];
        }
        else
        {
            //不需要 展开按钮
            _label.numberOfLines = 0;
            [_btn setIsEXP:NO];
            
            [_label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(@(titleSize.width));
                make.height.equalTo(@(titleSize.height));
            }];
        }
    }
    
    return self;
}

#pragma mark - cell btn click
-(void)cellBtnClick
{
    if (_btn.isEXP) {
        NSLog(@"EXP");
    }
    else
    {
        NSLog(@"NOEXP");
    }
    NSLog(@"----%f",self.frame.size.height);
}

-(CGFloat)returnRowHeight
{
    return height  ;
}

-(id)returnButton
{
    if (_btn != nil) {
        return _btn;
    }
    return nil;
}

@end
